package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.miaodong.entity.AppTask;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppTaskDao.java
 * @Description TODO
 * @createTime 2021年07月24日 18:05:00
 */
@Repository
public class AppTaskDao extends BaseDao<AppTask> {

    public final String FIELDS = "`id`, `taskname`, `jinbi`, `createtime`, `pic`, `typeStatus`, `taskstatus`, `userid`";

    /**
     * 数据新增 or保存
     * @param appTask
     */
    public void saveOrUpdate(AppTask appTask) {
        if (appTask.getId() == null || appTask.getId() == 0) {
            appTask.setId(this.add(appTask));
        } else {
            this.update(appTask);
        }
    }

    /**
     * 获取任务列表
     * @return
     */
    public List<AppTask> findList() {
        List<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_task where 1=1 ");
        String sql = str.toString();
        return this.queryList(sql, list, AppTask.class, null);
    }

    /**
     * 获取任务详情
     * @param id
     * @return
     */
    public AppTask getById(Integer id){
        if (id == null){
            return null;
        }
        return this.get(id,AppTask.class);
    }

    /**
     * 根据userid修改任务完成人
     */
    public void userFinalyTask(Integer id,String userid){
        StringBuilder sbStr = new StringBuilder("UPDATE app_task SET");
        ArrayList<Object> paramList = new ArrayList<Object>();
        sbStr.append(" userid = ?");
        paramList.add(userid);
        sbStr.append(" WHERE id = ? ");
        paramList.add(id);
        this.update(sbStr.toString(),paramList);
    }

    /**
     *
     * 根据任务状态获取任务详情
     */
    public AppTask getappTaskByTaskStatus(Integer type){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select " + FIELDS + " from app_task where 1=1");
        sbStr.append(" and typeStatus = ? ");
        paramList.add(type);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppTask.class);
    }

    public AppTask infoByTypeNameEn(String typeNameEn){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_task where tasknameEn=?");
        paramList.add(typeNameEn);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppTask.class);
    }

    /**
     * 将userid置为空,即当天没有用户开始做任务
     */
    public void updateTaskUserid(){
        String sql ="UPDATE app_task SET userid = null ";
        this.update(sql,null);
    }

}